using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 学期管理控制器
    /// </summary>
    /// <remarks>
    /// 该控制器提供学期信息的增删改查功能
    /// 包括分页查询、详情查询、创建、更新和删除操作
    /// </remarks>
    [Authorize]
    //[RequirePermission("basic.term")] // 指定所需的权限
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class TermController : ControllerBase
    {
        private readonly ITermService _termService;

        /// <summary>
        /// 构造函数，通过依赖注入获取学期服务
        /// </summary>
        /// <param name="termService">学期服务接口</param>
        public TermController(ITermService termService)
        {
            _termService = termService;
        }

        /// <summary>
        /// 获取学期分页列表
        /// </summary>
        /// <remarks>
        /// 根据请求参数返回分页的学期列表
        /// 支持按名称、简称和备注进行筛选
        /// </remarks>
        /// <param name="pageRequest">分页请求参数</param>
        /// <returns>学期分页列表</returns>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<TermDto>>> GetPagedList([FromBody] TermPageRequest pageRequest)
        {
            var result = await _termService.GetPagedListAsync(pageRequest);
            return ApiResponse<PagedResult<TermDto>>.Ok(result);
        }

        /// <summary>
        /// 获取学期详情
        /// </summary>
        /// <remarks>
        /// 根据学期ID获取学期的详细信息
        /// </remarks>
        /// <param name="request">ID请求参数</param>
        /// <returns>学期详情</returns>
        [HttpPost]
        public async Task<ApiResponse<TermDto>> GetById([FromBody] IdRequest request)
        {
            var term = await _termService.GetByIdAsync(request.Id);
            return ApiResponse<TermDto>.Ok(term);
        }

        /// <summary>
        /// 创建学期
        /// </summary>
        /// <remarks>
        /// 创建新的学期记录
        /// 会验证学期名称是否已存在
        /// </remarks>
        /// <param name="input">创建学期请求参数</param>
        /// <returns>创建后的学期详情</returns>
        [HttpPost]
        [RequirePermission("basic.term")] // 指定所需的权限
        public async Task<ApiResponse<TermDto>> Create([FromBody] CreateTermDto input)
        {
            var term = await _termService.CreateAsync(input);
            return ApiResponse<TermDto>.Ok(term);
        }

        /// <summary>
        /// 更新学期
        /// </summary>
        /// <remarks>
        /// 更新现有学期的信息
        /// 会验证学期是否存在以及名称是否重复
        /// </remarks>
        /// <param name="input">更新学期请求参数</param>
        /// <returns>更新后的学期详情</returns>
        [HttpPost]
        [RequirePermission("basic.term")] // 指定所需的权限
        public async Task<ApiResponse<TermDto>> Update([FromBody] UpdateTermDto input)
        {
            var term = await _termService.UpdateAsync(input);
            return ApiResponse<TermDto>.Ok(term);
        }

        /// <summary>
        /// 删除学期
        /// </summary>
        /// <remarks>
        /// 根据ID删除学期记录
        /// </remarks>
        /// <param name="request">ID请求参数</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [RequirePermission("basic.term")] // 指定所需的权限
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _termService.DeleteAsync(request.Id);
            return ApiResponse<object>.Ok(new object(), "删除成功");
        }
    }
}